package com.csnf.controller;

import csnf.com.entity.*;
import com.csnf.service.MerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

@RestController
@RequestMapping("/user")

public class MerchantController {
    @Autowired
    private MerchantService merchantService;
    @RequestMapping("/login")
    public ResponseObject login(@RequestBody Merchant merchant){
        ArrayList<Merchant> loginFlag = merchantService.selectAll(merchant);
        if(loginFlag!=null){
            return ResponseObject.success("true",loginFlag);
        }else{
            return ResponseObject.success("error");
        }
    }
    @RequestMapping("/register")
    public ResponseObject register(@RequestBody Merchant merchant) {
        merchant.setIsExamine(0);// 设置初始状态为未审核
        System.out.println("接收到的商户数据：" + merchant.toString());
        int registerFlag = merchantService.merchantRegister(merchant);
        if (registerFlag == 1) {
            return ResponseObject.success("success");
        } else {
            return ResponseObject.error("error");
        }
    }
    @RequestMapping("/selectMerchant")
    public ResponseObject selectMerchant(@RequestBody Merchant merchant) {
        Merchant merchant1 = merchantService.selectMerchant(merchant.getId());
        if(merchant1!=null){
            return ResponseObject.success("200",merchant1);
        }else {
            return ResponseObject.success("error");
        }
    }
    @RequestMapping("/updateMerchant")
    public ResponseObject updateMerchant(@RequestBody Merchant merchant) {
        merchant.setIsExamine(0);
        int updateMerchantFlag = merchantService.updateMerchant(merchant);
        if (updateMerchantFlag ==  1) {
            return ResponseObject.success("success");
        } else {
            return ResponseObject.error("error");
        }
    }
}
